package com.scm.controller;

/**
 * @author CHL
 * @ClassName: PaymentController
 * @Description: TODO
 * @date 2022/3/8 14:31
 * @Version 1.0
 */
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scm.dao.PayRecordMapper;
import com.scm.entity.PayRecord;
import com.scm.entity.response.ResponseMessage;
import com.scm.service.IPaymentService;
import com.scm.service.IPomainService;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/main/finance"})
public class PaymentController
{
    @Autowired
    private IPomainService pomainService;
    @Autowired
    private IPaymentService paymentService;
    @Autowired
    private PayRecordMapper payRecordMapper;
    private ResponseMessage rm = new ResponseMessage();

    @RequestMapping({"/pay"})
    public ResponseMessage payment(Long poId, String type, Integer payType, Integer page, HttpServletRequest req)
    {
        String user = (String)req.getAttribute("login");
        this.paymentService.pay(poId.longValue(), user, type);
        this.rm.setCode(Integer.valueOf(2));
        this.rm.setMessage("付款成功");
        this.rm.setData(this.pomainService.selectByStatus(page, Integer.valueOf(3), payType));
        return this.rm;
    }

    @RequestMapping({"/receipt"})
    public ResponseMessage receipt(Long soId, String type, HttpServletRequest req)
    {
        String user = (String)req.getAttribute("login");
        this.paymentService.rece(soId, user, type);
        this.rm.setCode(Integer.valueOf(2));
        this.rm.setMessage("收款成功");
        return this.rm;
    }

    @RequestMapping({"/query"})
    public Object queryRecord(String startDate, String endDate, String type, Integer payType, Long no, Integer page)
    {
        String mainTable = null;
        String field = null;
        if ("收入".equals(type))
        {
            mainTable = "somain";
            field = "soId";
        }
        else if ("支出".equals(type))
        {
            mainTable = "pomain";
            field = "poId";
        }
        else
        {
            this.rm.setCode(Integer.valueOf(3));
            this.rm.setMessage("收支类型为收入或支出");
            return this.rm;
        }
        HashMap<String, Object> m = new HashMap();
        m.put("startDate", startDate);
        m.put("endDate", endDate);
        m.put("type", type);
        m.put("no", no);
        m.put("payType", payType);
        m.put("mainTable", mainTable);
        m.put("field", field);
        PageHelper.startPage(page == null ? 1 : page.intValue(), 10);
        PageInfo<PayRecord> info = new PageInfo(this.payRecordMapper.selectRecord(m));
        return info;
    }
}

